package com.xuerhua.yxt.controller.student;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.KccsSignTime;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.reqVo.BaseQueryVO;
import com.xuerhua.yxt.reqVo.InterestCourseQueryVO;
import com.xuerhua.yxt.reqVo.KccsCourseQueryVO;
import com.xuerhua.yxt.reqVo.KccsStudentSignReqVO;
import com.xuerhua.yxt.service.IInterestCourseInfoService;
import com.xuerhua.yxt.service.IKccsCourseInfoService;
import com.xuerhua.yxt.service.IStudentService;
import com.xuerhua.yxt.utils.DateUtil;
import com.xuerhua.yxt.utils.WeekComparator;
import com.xuerhua.yxt.view.KccsCourseSignCountView;
import com.xuerhua.yxt.view.KccsCourseView;

@RequestMapping(Const.STUDENT_ROLE + MenuConst.INTEREST_COURSE)
@Controller
public class ChooseCourseControoller extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(ChooseCourseControoller.class);
	
	@Autowired
	private IInterestCourseInfoService interestCourseInfoService;
	@Autowired
	private IKccsCourseInfoService kccsCourseInfoService;
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(value={"","/"})
	public String index(Model model) {
		KccsSignTime date = kccsCourseInfoService.selectKccsEndDate(getAppUser().getParent().getSchoolId());
		if(DateUtil.compare(date.getEndTime(), new Date()) == 1 && 
		   DateUtil.compare(date.getStartTime(), new Date()) == -1) {
			model.addAttribute("isOk", true);
		}
		model.addAttribute("date", date);
		return "interestCourse/studentIndex";
	}
	
	@RequestMapping("detail/{courseId}")
	public String detail(Model model, @PathVariable Integer courseId) {
		AppUser appUser = getAppUser();
		KccsCourseQueryVO kccsCourseQueryVO = new KccsCourseQueryVO();
		kccsCourseQueryVO.setCourseId(courseId);
		kccsCourseQueryVO.setSchoolId(appUser.getTeacher().getSchoolId());
		model.addAttribute("detail", kccsCourseInfoService.selectViewById(kccsCourseQueryVO));
		return "kccsCourse/detail";
	}
	
	@RequestMapping("signed/list")
	@ResponseBody Object signedList() {
		AppUser appUser = getAppUser();
		KccsCourseQueryVO vo = new KccsCourseQueryVO();
		vo.setTermId(getCurrentTerm().getId());
		vo.setStudentId(appUser.getParent().getStudentId());
		return kccsCourseInfoService.selectSignListForParent(vo);
	}
	
	@RequestMapping("signCourseMap")
	@ResponseBody Object signCourseMap(KccsCourseQueryVO vo) {
		try {
			vo.setTermId(getCurrentTerm().getId());
			vo.setGradeName(getAppUser().getParent().getGradeName());
			List<KccsCourseView> list = kccsCourseInfoService.selectSignCourseList(vo);
			return new RspResult(Status.SUCCESS, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("signSubmit")
	@ResponseBody Object signSubmit(@RequestBody KccsStudentSignReqVO reqVO) {
		try {
			reqVO.setTermId(getCurrentTerm().getId());
			KccsSignTime endT = kccsCourseInfoService.selectKccsEndDate(reqVO.getSchoolId());
			if(DateUtil.compare(new Date(), endT.getEndTime()) == 1) {
				return new RspResult(Status.FAIL.getCode(),"本学期选修课报名已截止，有疑问请联系班主任");
			}
			if(DateUtil.compare(endT.getStartTime(), new Date()) == 1) {
				return new RspResult(Status.FAIL.getCode(),"本学期选修课报名还未开始，有疑问请联系班主任");
			}
			return kccsCourseInfoService.updateChangeSign(reqVO);
		} catch (Exception e) {
			logger.error("---signSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("list")
	public String list(Model model) {
		return "interestCourse/studentList";
	}
	
	@RequestMapping("list/query")
	@ResponseBody Object listQuery(InterestCourseQueryVO vo) {
		AppUser appUser = getAppUser();
		StudentInfo studentInfo = studentService.selectStudentBindParent(vo.getStudentId(), appUser.getOpenid());
		if(studentInfo == null) {
			return null;
		}
		vo.setClazz(studentInfo.getClazz());
		vo.setGrade(studentInfo.getGrade());
		vo.setTermId(getCurrentTerm().getId());
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setKey(vo.getCourseId() == null ? null : String.valueOf(vo.getCourseId()));
		return interestCourseInfoService.selectViewListForClass(vo);
	}
	
	@RequestMapping("detail/query")
	@ResponseBody Object detailQuery(Integer courseId) {
		AppUser appUser = getAppUser();
		KccsCourseQueryVO kccsCourseQueryVO = new KccsCourseQueryVO();
		kccsCourseQueryVO.setCourseId(courseId);
		kccsCourseQueryVO.setSchoolId(appUser.getTeacher().getSchoolId());
		kccsCourseQueryVO.setTermId(getCurrentTerm().getId());
		return kccsCourseInfoService.selectViewById(kccsCourseQueryVO);
	}
	
}
